<?php

class UsuarioController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    	
    }
	public function logoutAction()
	{
		Zend_Auth::getInstance()->clearIdentity();
		return $this->_redirect('/usuario/login');
	}
	public function loginAction()
	{
		$form = new Form_Usuario();
		 
		if( $this->getRequest()->isPost() )
		{			 
			if( $form->isValid( $this->_getAllParams() )) 
			{
				$autAdapter = new Zend_Auth_Adapter_DbTable();
				$autAdapter	->setTableName('users')
							->setIdentityColumn('username')
							->setCredentialColumn('password');
				$autAdapter	->setIdentity($form->getValue('username'))
							->setCredential($form->getValue('password'));
				$aut = Zend_Auth::getInstance();
				$result = $aut->authenticate($autAdapter);
				if( $result->isValid() )
				{
					return $this->_redirect('/posts/listar');
				}
				else
				{
					$form->username->addErrorMessage('Datos Incorrectos');
				}
			}
		}
		 
		$this->view->form = $form;
	}
}

